package emby.expr;

import emby.EmbyRuntime;
import emby.lang.Scope;
import emby.writer.IEmbyCodifier;

public class Variable extends Expression {

	private static final long serialVersionUID = 8965200488823246571L;
	private String variable;
	public Variable(String varname){
		variable = varname;
	}
	@Override
	public Object eval(Scope s) {
		if(EmbyRuntime.echo){
			emby.util.Print.println("Evaluating variable "+variable+" (result: "+s.getVar(getLine(),variable)+")");
		}
		return s.getVar(getLine(),variable);
	}

	public void setVar(Scope s, Object value){
		if(EmbyRuntime.echo){
			emby.util.Print.println("Assigning "+variable+" to expression "+value);
		}
		s.setVar(  variable , value);
	}

	/**
	 * Returns the variable's string name.
	 * @return
	 */
	public String getName(){
		return variable;
	}

	/**
	 * Returns whether this variable is read-only.
	 * <br>
	 * Read only variables start with a $ symbol in their name and
	 * cannot be found on the left hand-side of an assignment statement
	 * or else it is a syntax error.
	 * @return
	 */
	public boolean isReadOnly(){
		if (variable.startsWith("$"))
			return true;
		return false;
	}

	public String toString(){
		return "{Variable: "+variable+"}";
	}
 
	public void codify(IEmbyCodifier ec){
		ec.variable(variable);
	}
}
